package icegps.com.pingdj.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import icegps.com.pingdj.App;
import icegps.com.pingdj.R;
import icegps.com.pingdj.adapter.MeasureDataAdapter;
import icegps.com.pingdj.constant.Token;
import icegps.com.pingdj.gen.MapMeasureInfoDao;
import icegps.com.pingdj.map.bean.MapMeasureInfo;
import icegps.com.pingdj.map.utils.DataManager;
import icegps.com.pingdj.utils.DateFormat;
import icegps.com.pingdj.utils.Utils;
import icegps.com.pingdj.dialog.BottomDialog;
import timber.log.Timber;

import static icegps.com.pingdj.constant.ActionKey.BPOS;
import static icegps.com.pingdj.constant.ActionKey.MEASURE_AVE_HEIGHT;
import static icegps.com.pingdj.constant.ActionKey.TITLE;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;

/**
 * Created by 111 on 2018/3/14.
 */

public class MeasureDataActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    private MeasureDataAdapter measureDataAdapter;
    private List<MapMeasureInfo> mapMeasureInfos;
    private MapMeasureInfoDao mapMeasureInfoDao;
    private BottomDialog bottomDialog;
    private int position;


    @Override
    protected String setTitle() {
        return getString(R.string.measure_data);
    }

    @Override
    protected void init() {
        mapMeasureInfoDao = App.getInstance().getDaoSession().getMapMeasureInfoDao();
        initDialog();
        //不加载上次作业的数据
        mapMeasureInfos = mapMeasureInfoDao.queryBuilder()
                .where(MapMeasureInfoDao.Properties.Id.notEq(Token.CONTINUE_WORK_TOKEN)).list();


        if (mapMeasureInfos != null && mapMeasureInfos.size() > 0) {
            Collections.sort(mapMeasureInfos);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setLayoutManager(new LinearLayoutManager(activity));
            measureDataAdapter = new MeasureDataAdapter(R.layout.item_measure, mapMeasureInfos);
            measureDataAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            recyclerView.setAdapter(measureDataAdapter);
            measureDataAdapter.setOnItemClickListener(this);
            tvEmpty.setVisibility(View.GONE);
        }
    }

    private void initDialog() {

        View inflate = View.inflate(activity, R.layout.dialog_bottom, null);
        View look = inflate.findViewById(R.id.ll_look);
        View plane = inflate.findViewById(R.id.ll_use_plane);
        View delete = inflate.findViewById(R.id.ll_delete);
        bottomDialog = new BottomDialog(activity);
        bottomDialog.setContentView(inflate);
        look.setOnClickListener(v -> {
            bottomDialog.dismiss();
            startActivity(mapMeasureInfos.get(position).getId(), DateFormat.time2Date(mapMeasureInfos.get(position).getId()));
        });

        plane.setOnClickListener(v -> {
            bottomDialog.dismiss();
            double[] b = new double[3];
            Intent intent = new Intent(activity, PlaneActivity.class);
            intent.putExtra(LOAD_TOKEN, mapMeasureInfos.get(position).getId());
            intent.putExtra(MEASURE_AVE_HEIGHT, mapMeasureInfos.get(position).getAve());
            intent.putExtra(BPOS, b);
            startActivity(intent);
        });

        delete.setOnClickListener(v -> {
            bottomDialog.dismiss();
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(R.string.confirm_delete)
                    .setNegativeButton(R.string.cancel, null)
                    .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DataManager.deleteDataByToken(mapMeasureInfos.get(position).getId());
                            mapMeasureInfos.remove(position);
                            measureDataAdapter.notifyDataSetChanged();
                            if (mapMeasureInfos.size() == 0) {
                                recyclerView.setVisibility(View.GONE);
                                tvEmpty.setVisibility(View.VISIBLE);
                            }
                            Utils.showToast(getString(R.string.delete_success));
                        }
                    }).show();
        });
    }

    private void startActivity(long token, String title) {
        Intent intent = new Intent(activity, MeasureDataMapActivity.class);
        intent.putExtra(LOAD_TOKEN, token);
        intent.putExtra(TITLE, title);
        startActivity(intent);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_measure_data;
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        this.position = position;
        bottomDialog.show();
    }

}
